﻿package code {
	// class acts as game spinner that rotates
	public class Spinner extends GameObject{
		public var theta:Number;
		public var omega:Number;
		public var mass:Number;
		
		private const FRICTION = .95
		// creates a spinner
		public function Spinner(shapeType:String, radius:Number, sides:Number, color:uint, thickness:Number, hollow:Boolean, spinnerLength:Number) {
			super(shapeType, radius, sides, color, thickness, hollow, 0, 0, null, spinnerLength);
			mass = 2000;  // large to compensate for dT
			omega = 0;
			theta= 0;
		}
		
		// rotaets the spinner by applying an omega value
		public function rotateSpinner(dT:Number){
			shape.graphics.clear();
			shape.angle += omega * dT;  // euler intergration
			shape.rotateVertices();  // rerotates verticse
			shape.renderVertices();	 // renderds them
			omega *= FRICTION;  // friction value to slow down 
			
			shape.linesArray = createLinesArray();
		}
		
		// creates the sepereate lines array for spinners
		override public function createLinesArray():Array{
			var verticesToPush = new Array();  // array of vertices to check.  Kept seperate from main vertices of shape
			var linesToPush = new Array();	// the lines to push in the main game class. A debugging feature to see how it would work
			
			if(shape.vertices.length > 0){  // to prevent a glitch where the we were getting null vertices
				for each(var v:Vertex in shape.vertices){
					verticesToPush.push(new Vertex(v.x + x, v.y + y)); // pushes all the vertices
				}
				verticesToPush.push(new Vertex(shape.vertices[0].x + x, shape.vertices[0].y + y));  // pushes the first vertex so it will loop back
				for(var i:int = 0; i < verticesToPush.length - 1; i++){	// goes through the vertices and creates line out of them
					var tempLine:Line = new Line(new Vertex(x, y), verticesToPush[i + 1], 3, 0x000000);  // creates the temp line to push from origin to vertex going out to
					tempLine.go = this; // sets this to the gameobject to know about in collision response
					shape.linesArray.push(tempLine);  // stores it in shape
					linesToPush.push(tempLine);  // puts them into the output debug array
				}
			}
			return linesToPush;
		}
		
		// gets the intertia of the bar with (2/3)mL^2
		public function getInertia(leverArmDisplacement:Number){
			return ((2 /3) * mass * Math.pow(leverArmDisplacement, 2));
		}

	}
	
}
